home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src_ansi / ace / c / serial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-05  |  6.2 KB  |  296 lines

  1. /* << ACE >>
  2.  
  3.    -- Amiga BASIC Compiler --
  4.  
  5.    ** Parser: serial port commands **
  6.    ** Copyright (C) 1998 David Benn
  7.    ** 
  8.    ** This program is free software; you can redistribute it and/or
  9.    ** modify it under the terms of the GNU General Public License
  10.    ** as published by the Free Software Foundation; either version 2
  11.    ** of the License, or (at your option) any later version.
  12.    **
  13.    ** This program is distributed in the hope that it will be useful,
  14.    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    ** GNU General Public License for more details.
  17.    **
  18.    ** You should have received a copy of the GNU General Public License
  19.    ** along with this program; if not, write to the Free Software
  20.    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    Author: David J Benn
  23.    Date: 5th,6th September 1993
  24.  */
  25.  
  26. #include "acedef.h"
  27. #include <string.h>
  28.  
  29. /* locals */
  30. static char *frame_ptr[] = {"(a4)", "(a5)"};
  31.  
  32. /* externals */
  33. extern int sym;
  34. extern int lev;
  35. extern int obj;
  36. extern int typ;
  37. extern SYM *curr_item;
  38. extern char id[MAXIDSIZE];
  39.  
  40. /* functions */
  41. void serial_command (void)
  42. {
  43. /* parse a serial command */
  44.  
  45.   insymbol ();
  46.  
  47.   switch (sym)
  48.     {
  49.     case opensym:
  50.       open_serial ();
  51.       break;
  52.  
  53.     case closesym:
  54.       close_serial ();
  55.       break;
  56.  
  57.     case readsym:
  58.       read_serial ();
  59.       break;
  60.  
  61.     case writesym:
  62.       write_serial ();
  63.       break;
  64.  
  65.     default:
  66.       _error (75);        /* open,close etc expected */
  67.       break;
  68.     }
  69. }
  70.  
  71. void open_serial (void)
  72. {
  73. /* open a channel to a serial port.
  74.  
  75.    SERIAL OPEN [#] channel,unit,baud,"N|E|O|M|S<d><s>[A][X]"[,size][,name]
  76.  
  77.    where:       channel = unique channel number 
  78.    unit = unit of serial device to use
  79.    baud = baud rate
  80.    NEOMS        = parity: None, Even, Odd, Mark, Space
  81.    <d>  = # of data bits (1 digit)
  82.    <s>  = # of stop bits (1 digit)
  83.    A    = serial device will use ALL 7 port wires
  84.    X    = sets Xon/Xoff mode
  85.    size = size of serial buffer
  86.    name = name of serial device         
  87.  */
  88.  
  89.   insymbol ();
  90.  
  91.   if (sym == hash)
  92.     insymbol ();        /* # */
  93.  
  94.   if (make_integer (expr ()) == shorttype)
  95.     make_long ();        /* channel */
  96.  
  97.   if (sym != comma)
  98.     _error (16);
  99.   else
  100.     {
  101.       insymbol ();
  102.       if (make_integer (expr ()) == shorttype)
  103.     make_long ();        /* unit */
  104.  
  105.       if (sym != comma)
  106.     _error (16);
  107.       else
  108.     {
  109.       insymbol ();
  110.       if (make_integer (expr ()) == shorttype)
  111.         make_long ();    /* baud */
  112.  
  113.       if (sym != comma)
  114.         _error (16);
  115.       else
  116.         {
  117.           insymbol ();
  118.           if (expr () != stringtype)
  119.         _error (4);    /* parameter string */
  120.           else
  121.         {
  122.           /* optional serial READ buffer size */
  123.           if (sym == comma)
  124.             {
  125.               insymbol ();
  126.               if (sym != comma)
  127.             {
  128.               if (make_integer (expr ()) == shorttype)
  129.                 make_long ();    /* Read buffer size */
  130.             }
  131.               else
  132.             gen ("move.l", "#512", "-(sp)");    /* defaults to 512 bytes */
  133.             }
  134.           else
  135.             gen ("move.l", "#512", "-(sp)");    /* defaults to 512 bytes */
  136.  
  137.           /* optional serial device name */
  138.           if (sym == comma)
  139.             {
  140.               insymbol ();
  141.               if (expr () != stringtype)
  142.             _error (4);    /* serial device name */
  143.             }
  144.           else
  145.             gen ("move.l", "#0", "-(sp)");    /* defaults to NULL */
  146.  
  147.           /* call open_serial function */
  148.           gen ("jsr", "_OpenSerial", "  ");
  149.           gen ("add.l", "#24", "sp");
  150.           enter_XREF ("_OpenSerial");
  151.         }
  152.         }
  153.     }
  154.     }
  155. }
  156.  
  157. void close_serial (void)
  158. {
  159. /* close a channel to a serial port. 
  160.  
  161.    SERIAL CLOSE [#] channel
  162.  */
  163.  
  164.   insymbol ();
  165.  
  166.   if (sym == hash)
  167.     insymbol ();        /* # */
  168.  
  169.   if (make_integer (expr ()) == shorttype)
  170.     make_long ();        /* channel */
  171.  
  172.   gen ("jsr", "_CloseSerial", "  ");
  173.   gen ("addq", "#4", "sp");
  174.   enter_XREF ("_CloseSerial");
  175. }
  176.  
  177. void read_serial (void)
  178. {
  179.   SYM *storage;
  180.   char addrbuf[40];
  181.  
  182. /* read a specified number of bytes into a buffer.
  183.  
  184.    SERIAL READ [#] channel,buffer,length
  185.  */
  186.  
  187.   insymbol ();
  188.  
  189.   if (sym == hash)
  190.     insymbol ();        /* # */
  191.  
  192.   if (make_integer (expr ()) == shorttype)
  193.     make_long ();        /* channel */
  194.  
  195.   if (sym != comma)
  196.     _error (16);
  197.   else
  198.     {
  199.       insymbol ();
  200.       if (sym == ident && obj == variable)    /* buffer */
  201.     {
  202.       /* if string variable/array doesn't exist, create a simple variable */
  203.       if (!exist (id, variable) && !exist (id, array))
  204.         {
  205.           /* allocate a simple string variable */
  206.           enter (id, typ, obj, 0);
  207.           enter_DATA("_nullstring:", "dc.b 0");
  208.           gen ("pea", "_nullstring", "  ");
  209.           assign_to_string_variable (curr_item, MAXSTRLEN);
  210.         }
  211.  
  212.       storage = curr_item;
  213.  
  214.       /* is it a string variable or array? */
  215.       if (storage->type != stringtype)
  216.         _error (4);
  217.       else
  218.         {
  219.           /* get address of string pointed to by variable/array element */
  220.           itoa (-1 * storage->address, addrbuf, 10);
  221.           strcat (addrbuf, frame_ptr[lev]);
  222.  
  223.           /* pass string address to function (on stack) */
  224.           if (storage->object == array)
  225.         {
  226.           point_to_array (storage, addrbuf);
  227.           gen ("move.l", addrbuf, "d0");
  228.           gen ("add.l", "d7", "d0");
  229.           gen ("move.l", "d0", "-(sp)");
  230.         }
  231.           else
  232.         gen ("move.l", addrbuf, "-(sp)");
  233.  
  234.           insymbol ();
  235.           if (sym != comma)
  236.         _error (16);
  237.           else
  238.         {
  239.           insymbol ();
  240.           if (make_integer (expr ()) == shorttype)
  241.             make_long ();    /* length */
  242.  
  243.           /* call serial_read function */
  244.           gen ("jsr", "_ReadSerial", "  ");
  245.           gen ("add.l", "#12", "sp");
  246.           enter_XREF ("_ReadSerial");
  247.         }
  248.         }
  249.     }
  250.       else
  251.     _error (19);        /* variable (or array) expected */
  252.     }
  253.   insymbol ();
  254. }
  255.  
  256. void write_serial (void)
  257. {
  258. /* write a specified number of bytes from a buffer.
  259.  
  260.    SERIAL WRITE [#] channel,buffer,length
  261.  */
  262.  
  263.   insymbol ();
  264.  
  265.   if (sym == hash)
  266.     insymbol ();        /* # */
  267.  
  268.   if (make_integer (expr ()) == shorttype)
  269.     make_long ();        /* channel */
  270.  
  271.   if (sym != comma)
  272.     _error (16);
  273.   else
  274.     {
  275.       insymbol ();
  276.       if (expr () != stringtype)
  277.     _error (4);        /* buffer */
  278.       else
  279.     {
  280.       if (sym != comma)
  281.         _error (16);
  282.       else
  283.         {
  284.           insymbol ();
  285.           if (make_integer (expr ()) == shorttype)
  286.         make_long ();    /* length */
  287.  
  288.           /* call serial_write function */
  289.           gen ("jsr", "_WriteSerial", "  ");
  290.           gen ("add.l", "#12", "sp");
  291.           enter_XREF ("_WriteSerial");
  292.         }
  293.     }
  294.     }
  295. }
  296.